
let argv = process.argv.slice(2)

// const { webpack } = require('./webpack'); //手写webpack

const webpack = require("webpack");
const webpackDevServer = require('webpack-dev-server');
const webpackOptions = require("./build/webpack.config.js"); //这里一般会放配置信息
const compiler = webpack(webpackOptions);

// --dev--

if (argv.includes('--mode=development')) {
    const server = new webpackDevServer(webpackOptions.devServer, compiler)
    server.start().then(async res => {
        const localIPv4 = await webpackDevServer.internalIP('v4');
        const localIPv6 = await webpackDevServer.internalIP('v6');

        // console.log('Local IPv4 address:', localIPv4);
        // console.log('Local IPv6 address:', localIPv6);
    })
}

// --build--
if (argv.includes('--mode=production')) {
    compiler.run((err, stats) => {
        if (err) {
            console.error(err.stack || err);
            if (err.details) {
                console.error(err.details);
            }
            return;
        }

        const info = stats.toJson();

        if (stats.hasErrors()) {
            console.error('errors => ', info.errors);
        }

        if (stats.hasWarnings()) {
            console.warn('warnings => ', info.warnings);
        }

        stats = stats.toJson({
            assets: true, //打印本次编译产出的资源
            chunks: true, //打印本次编译产出的代码块
            modules: true, //打印本次编译产出的模块
        })
        // console.log('stats => ', stats);
        // console.log('assets => ', stats.assets);
        // console.log('chunks => ', stats.chunks);
        // console.log('origins => ', stats.chunks[0].origins);
    });
}
